package de.fh.ka.as.car.drivecontrol;

import ioio.lib.api.AnalogInput;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.AbstractIOIOActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import de.fh.ka.as.car.R;
import de.fh.ka.as.car.drivecontrol.model.Car;
import de.fh.ka.as.car.drivecontrol.model.Control;
import de.fh.ka.as.car.hardware.Engine;
import de.fh.ka.as.car.hardware.Engine.Direction;
import de.fh.ka.as.car.hardware.Steering;

/**
 * This is the main activity of the HelloIOIO example application.
 * 
 * It displays a toggle button on the screen, which enables control of the on-board LED. This example shows a
 * very simple usage of the IOIO, by using the {@link AbstractIOIOActivity} class. For a more advanced use
 * case, see the HelloIOIOPower example.
 */
public class RealCar extends AbstractIOIOActivity implements Car {

	private final int engineServoPort = 3;
	private final int steeringServoPort = 4;
	private final int lightRightPort = 5;
	private final int lightLeftPort = 6;

	private Engine engine;
	private Steering steering;

	protected Control control = new Control();

	private MediaPlayer player;
	protected View view2;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setView();
	}

	protected void setView() {
		CanvasView canvasView = new CanvasView(this, this);
		view2 = canvasView;
		setContentView(canvasView);
		// canvasView.getHorn().addActionListener(new ActionListener() {
		//
		// @Override
		// public void actionEventPerformed(ActionEvent event) {
		// player = MediaPlayer.create(getBaseContext(), R.raw.horn);
		// player.start();
		// Log.i("Hupe", "QuickDirtyRealCar: Hupe ist " + control.isHorn());
		//
		// }
		// });
	}

	/**
	 * A method to create our IOIO thread.
	 * 
	 * @see ioio.lib.util.AbstractIOIOActivity#createIOIOThread()
	 */
	@Override
	protected AbstractIOIOActivity.IOIOThread createIOIOThread() {
		return new IOIOThread();
	}

	@Override
	public void control(Control control) {
		this.control = control;
	}

	/**
	 * This is the thread on which all the IOIO activity happens. It will be run every time the application is
	 * resumed and aborted when it is paused. The method setup() will be called right after a connection with
	 * the IOIO has been established (which might happen several times!). Then, loop() will be called
	 * repetitively until the IOIO gets disconnected.
	 */
	class IOIOThread extends AbstractIOIOActivity.IOIOThread {
		/** The on-board LED. */
		private DigitalOutput ioioLED;

		private DigitalOutput lightRight;
		private DigitalOutput lightLeft;

		private AnalogInput sensor;

		/**
		 * Called every time a connection with IOIO has been established. Typically used to open pins.
		 * 
		 * @throws ConnectionLostException
		 *            When IOIO connection is lost.
		 * 
		 * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#setup()
		 */
		@Override
		protected void setup() throws ConnectionLostException {
			ioioLED = ioio_.openDigitalOutput(0, true);

			lightRight = ioio_.openDigitalOutput(lightRightPort, true);
			lightLeft = ioio_.openDigitalOutput(lightLeftPort, true);
			engine = new Engine(ioio_, engineServoPort);
			steering = new Steering(ioio_, steeringServoPort);

			sensor = ioio_.openAnalogInput(46);
			// ioio_.openDigitalInput(7);
		}

		/**
		 * Called repetitively while the IOIO is connected.
		 * 
		 * @throws ConnectionLostException
		 *            When IOIO connection is lost.
		 * 
		 * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#loop()
		 */
		@Override
		protected void loop() throws ConnectionLostException {
			ioioLED.write(control.isBrake());

			engine.setSpeed(control.isBrake() ? 0 : control.getSpeed(), Direction.Forward);
			steering.setSteering(control.getSteering());

			if (control.isHorn()) {
				player = MediaPlayer.create(getBaseContext(), R.raw.horn);
				player.start();
			}

			lightLeft.write(control.isLightLeft());
			lightRight.write(control.isLightRight());
			try {
				sleep(10);
			} catch (InterruptedException e) {
			}
		}
	}
}
